// pages/Systems/FeedbackDetail/FeedbackDetail.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getGoodsOrder } from '../../../api/order';
import { updateUserSuggestions } from '../../../api/brand';
import { checkFile } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    detail: {},
    order: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    if(options.item){
      var item = JSON.parse(options.item)
      console.log(item,'item')
      if(item.imgUrls){
        item.imgUrls = _this.imgUrlDis(item.imgUrls)
      }
      if(item.userSubmitComplaintSuggestionsInfoDTOList){
        for (let index = 0; index < item.userSubmitComplaintSuggestionsInfoDTOList.length; index++) {
          const itm = item.userSubmitComplaintSuggestionsInfoDTOList[index];
          if(itm.contentImgUrl){
            itm.contentImgUrl = _this.imgUrlDis(itm.contentImgUrl);
          }
        }
      }
      this.setData({
        detail: item,
      })
      if(item.orderCode){
        this._getOrders();
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 查询订单数据
  _getOrders(){
    let _this = this;
    getGoodsOrder({goodsName: _this.data.goodsName,page: _this.data.page,orderCode: _this.data.detail.orderCode})
    .then(async (res) => {
      if(res.success){
        var data = [];
        for (let index = 0; index < res.data.content.length; index++) {
          const item = res.data.content[index];
          item.goodsInfo = JSON.parse(item.goodsInfo);
          item.receivingAddressInfo = JSON.parse(item.receivingAddressInfo);
          data.push(item);
        }
        _this.setData({
          order: data[0],
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 图片处理
  imgUrlDis(imgurl){
    var arr = [];
    console.log(typeof(imgurl),'typeof(imgurl)')
    if(typeof(imgurl) == 'string'){
      var array = imgurl.split(',');
      for (let index = 0; index < array.length; index++) {
        const img = array[index];
        var types = checkFile(img);
        if(types == 'video'){
          arr.push({url: img,isVideo: true,deletable: true})
        }else if(types == 'image'){
          arr.push({url: img,isImage: true,deletable: true})
        }
      }
    }else if(typeof(imgurl) == 'object'){
      for (let index = 0; index < imgurl.length; index++) {
        const img = imgurl[index];
        var types = checkFile(img);
        if(types == 'video'){
          arr.push({url: img,isVideo: true,deletable: true})
        }else if(types == 'image'){
          arr.push({url: img,isImage: true,deletable: true})
        }
      }
    }
    return arr;
  },
  // 当前反馈已解决
  settle(){
    let _this = this;
    const { detail } = _this.data;
    updateUserSuggestions({orderCode: detail.orderCode,userId: detail.userId,id: detail.id,phone: detail.phone,solveStatus: '1',})
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '操作成功',
          icon: 'success',
        })
        wx.navigateBack({
          delta: 1,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // console.log(item,'item')
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
})